package com.good.msgsys.service.impl;

import com.good.msgsys.dao.AwardDao;
import com.good.msgsys.dao.OpusDao;
import com.good.msgsys.entity.Award;
import com.good.msgsys.entity.Opus;
import com.good.msgsys.service.OpusService;
import com.good.msgsys.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author junlong
 * @date 2020/12/31 11:08
 */
@Service
public class OpusServiceImpl implements OpusService {
    @Autowired
    private OpusDao opusDao;
    @Autowired
    private AwardDao awardDao;
    @Autowired
    private IdWorker idWorker;

    @Override
    @Transactional
    public void add(Opus opus) {
        // 成果
        Award award = new Award();
        String awardId = idWorker.nextId() + "";
        award.setId(awardId);
        // 著作
        String id = idWorker.nextId() + "";
        opus.setId(id);
        opus.setState(0); // 默认新增著作时，状态为0：未提交
        award.setOpusId(opus.getId());
        // 保存
        opusDao.save(opus);
        awardDao.save(award);
    }

    @Override
    public void deleteById(String id) {
        opusDao.deleteById(id);
    }

    @Override
    public void update(Opus opus) {
        Opus temp = opusDao.findById(opus.getId()).get();
        temp.setName(opus.getName());
        opusDao.save(temp);
    }

    @Override
    public Opus findById(String id) {
        return opusDao.findById(id).get();
    }

    @Override
    public List<Opus> findAll() {
        return opusDao.findAll();
    }
}
